import { amplifyAppReact, amplifyModelgen, amplifyPush, addIntegAccountInConfig } from '../../amplify-app-helpers/amplify-app-setup';
import { createNewProjectDir, deleteProject, deleteProjectDir, getCLIPath } from '@aws-amplify/amplify-e2e-core';
import { AmplifyFrontend } from '@aws-amplify/amplify-cli-core';
import {
  validateProject,
  validateProjectConfig,
  validateApi,
  validateBackendConfig,
  validateModelgen,
  validateAmplifyPush,
  validateFeatureFlags,
} from '../../amplify-app-helpers/amplify-app-validation';
import execa from 'execa';

describe('amplify-app platform tests', () => {
  let projRoot: string;

  beforeAll(async () => {
    const cliPath = getCLIPath();
    const cliVersion = (await execa(cliPath, ['version'])).stdout;
    // Print CLI path, CLI version and process.env.AMPLIFY_PATH for reference
    // Code generated by amplify-app honors AMPLIFY_PATH env variable
    console.log(`amplify-app tests: CLI path ${cliPath}, AMPLIFY_PATH=${process.env.AMPLIFY_PATH}, CLI version ${cliVersion}`);
  });

  beforeEach(async () => {
    projRoot = await createNewProjectDir('amplify-app');
  });

  afterEach(() => {
    deleteProjectDir(projRoot);
  });

  jest.setTimeout(1000 * 60 * 30); // 30 minutes is enough as push operations are taking time

  it('should set up a react project and run scripts', async () => {
    await amplifyAppReact(projRoot);
    validateProject(projRoot, AmplifyFrontend.javascript);
    validateProjectConfig(projRoot, AmplifyFrontend.javascript, 'react');
    validateApi(projRoot);
    validateBackendConfig(projRoot);
    validateFeatureFlags(projRoot);
    addIntegAccountInConfig(projRoot);
    await amplifyModelgen(projRoot);
    validateModelgen(projRoot);
    await amplifyPush(projRoot);
    validateAmplifyPush(projRoot);
    await deleteProject(projRoot);
  });
});
